package com.hsxc.cdz.controller.appController;

import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.MyUser;
import com.hsxc.cdz.base.util.CheckMessage;
import com.hsxc.cdz.mgmt.mobileMessage.MobileMessage;
import com.hsxc.cdz.mgmt.user.MyUserManager;

import net.sf.json.JSONObject;

/**
 * @author wpfei APP用户手机验证码验证和用户密码重置
 */
@Controller
@RequestMapping("/account")
public class AppMessController {
	protected static Logger logger = Logger.getLogger("AppMessController");
	@Autowired
	MyUserManager myUserManager;

	/*
	 * 根据手机号码获取验证码
	 */
	@RequestMapping(value = "/mobileMessage", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject getMobileMessage(@RequestParam("tel") String tel, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		logger.debug("手机号码" + tel);
		httpSession.setAttribute("tel", tel.trim());
		logger.debug("得到session中的tel" + (String) httpSession.getAttribute("tel"));
		if (!CheckMessage.isPhone(tel)) {
			result.put("success", false);
			result.put("message", "该手机号码不合法");
			logger.info("message" + "该手机号码不合法");
			return result;
		}
		// MyUser user = myUserManager.loadMyUserByMobile(tel.trim());
		// if (user == null) {
		// 只要请求发送验证码都发，
		// 注册时，在第三步时再判断用户是否已经存在
		// 与找回密码共用一个发送验证码的请求
		MobileMessage.sendMsg(tel);
		result.put("success", true);
		result.put("message", "mobileMessage请求成功");
		logger.info("message" + "mobileMessage请求成功");
		return result;
		// }else{

		// }

	}

	/*
	 * 校验验证码
	 */
	@RequestMapping(value = "/checkMobileMessage", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject checkMobileMessage(@RequestBody String jsonObj, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		org.json.JSONObject jsonObject = null;
		try {
			jsonObject = new org.json.JSONObject(jsonObj);
			String vireCode = jsonObject.getString("code").trim();
			logger.debug("vireCode为" + vireCode);
			String tel = jsonObject.getString("tel").trim();
			logger.debug("得到的手机号码"+tel);
			boolean checkResult = MobileMessage.checkMsg(tel, vireCode);
			if (checkResult) {
				result.put("success", true);
				result.put("message", "checkMobileMessage请求成功");
				logger.info("message" + "手机和验证码验证成功");
			} else {
				result.put("success", false);
				result.put("message", "验证码错误");
				logger.info("message" + "手机验证码错误");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("json数据解析失败" + e.getMessage());
		}
		return result;

	}

	/*
	 * 修改用户密码
	 */
	@RequestMapping(value = "/resetPasswordRequest", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject updateSecret(@RequestBody String obj, HttpSession httpSession) {
		JSONObject result = new JSONObject();
		MyUser myUser = new MyUser();
		MyUser user = new MyUser();
		int ii = -1;
		org.json.JSONObject jsonObject;

		try {
			jsonObject = new org.json.JSONObject(obj);
			// String tel=jsonObject.getString("tel").trim();
			String tel = (String) httpSession.getAttribute("tel");
			httpSession.setAttribute("tel", null);
			logger.info("手机号码为" + tel);
			user = myUserManager.loadMyUserByMobile(tel.trim());
			logger.info("得到的对象为" + myUser);
			String password = jsonObject.getString("password").trim();
			if (user != null) {
				myUser.setPassword(password);
				myUser.setTel(tel.trim());
				ii = myUserManager.updateUserByTel(myUser);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (ii >= 0) {
			result.put("success", true);
			result.put("message", "密码更新成功");
			logger.info("message:" + "密码更新成功");
		} else {
			result.put("success", false);
			result.put("message", "密码更新失败");
			logger.info("message:" + "密码更新失败");
		}

		return result;

	}
}
